package com.mp.approvalreq.util;

import com.alibaba.fastjson.JSONObject;
import com.mp.approvalreq.entity.ContentType;
import com.mp.approvalreq.entity.GroupContent;
import com.mp.approvalreq.entity.TemplateComponent;
import com.plusmoney.util.Utils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ComponentUtils {

    public static Map<Integer, String> getComponentValueMap(List<TemplateComponent> componentsContents) {
        if (Utils.isEmpty(componentsContents)) {
            return new HashMap<>();
        }
        return componentsContents.stream().filter(o -> o.getValue() != null).collect(Collectors.toMap(TemplateComponent::getNum, TemplateComponent::getValue, (a, b) -> b));
    }

    public static Map<Integer, TemplateComponent> getComponentMap(List<TemplateComponent> componentsContents) {
        if (Utils.isEmpty(componentsContents)) {
            return new HashMap<>();
        }
        return componentsContents.stream().collect(Collectors.toMap(TemplateComponent::getNum, componentsContent -> componentsContent, (a, b) -> b));
    }

    public static Map<String, TemplateComponent> getComponentNameMap(List<TemplateComponent> componentsContents) {
        if (Utils.isEmpty(componentsContents)) {
            return new HashMap<>();
        }
        return componentsContents.stream().collect(Collectors.toMap(TemplateComponent::getName, componentsContent -> componentsContent, (a, b) -> b));
    }

    public static Map<Integer, List<TemplateComponent>> getRequestIdComponentMap(List<TemplateComponent> componentsContents) {
        Map<Integer, List<TemplateComponent>> map = new HashMap<>();
        for (TemplateComponent component : componentsContents) {
            if (!map.containsKey(component.getRequestId())) {
                map.put(component.getRequestId(), new ArrayList<>());
            }
            map.get(component.getRequestId()).add(component);
        }
        return map;
    }

    /**
     * 计算组件列表中指定类型的组件数量
     *
     * @param componentsContents
     * @param contentType
     * @return
     */
    public static int countByContentType(List<TemplateComponent> componentsContents, int contentType) {
        if (Utils.isEmpty(componentsContents)) {
            return 0;
        }
        return (int) componentsContents.stream().filter(componentsContent -> componentsContent.getType() == contentType).count();
    }

    /**
     * 获取指定类型的组件
     *
     * @param componentsContents
     * @param contentType
     * @return
     */
    public static List<TemplateComponent> getComponentByType(List<TemplateComponent> componentsContents, int contentType) {
        if (Utils.isEmpty(componentsContents)) {
            return new ArrayList<>();
        }
        return componentsContents.stream().filter(componentsContent -> componentsContent.getType() == contentType).collect(Collectors.toList());
    }

    /**
     * 获取指定位置的组件
     *
     * @param componentsContents
     * @param number
     * @return
     */
    public static TemplateComponent getComponentByNumber(List<TemplateComponent> componentsContents, int number) {
        if (Utils.isEmpty(componentsContents)) {
            return null;
        }
        return componentsContents.stream().filter(componentsContent -> componentsContent.getNum() == number).findFirst().orElse(null);
    }

    /**
     * 合并模板与组件的值（把组件的值填入模板）
     *
     * @param templateComponents
     * @param components
     * @return
     */
    public static List<TemplateComponent> mergeComponents(List<TemplateComponent> templateComponents, List<TemplateComponent> components) {
        Map<Integer, String> componentMap = getComponentValueMap(components);
        templateComponents.forEach(component -> component.setValue(componentMap.get(component.getNum())));
        return templateComponents;
    }

    public static List<TemplateComponent> mergeComponents(List<TemplateComponent> templateComponents, List<TemplateComponent> components,
                                                          Map<Integer, List<GroupContent>> groupContentMap) {
        Map<Integer, String> componentMap = getComponentValueMap(components);
        for (TemplateComponent component : templateComponents) {
            if (component.getType() == ContentType.COMPONENT_GROUP) {
                if (Utils.isNotNull(groupContentMap)) {
                    List<GroupContent> groupContents = groupContentMap.get(component.getNum());
                    if (Utils.isNotEmpty(groupContents)) {
                        component.setValue(JSONObject.toJSONString(groupContents));
                        component.setGroupContents(groupContents);
                    }
                }
            } else {
                component.setValue(componentMap.get(component.getNum()));
            }
        }
        return templateComponents;
    }

    public static boolean existGroupComponent(List<TemplateComponent> components) {
        if (Utils.isEmpty(components)) {
            return false;
        }
        return components.stream().anyMatch(component -> component.getType() == ContentType.COMPONENT_GROUP);
    }
}
